// ============================================================================
//
// LCP-net framework by Pierre CHATEL chatelp@gmail.com
// Distributed under GPLv3
// Copyright (C) THALES Land & Joint Systems, 2008, 2009. All rights reserved
//
// ============================================================================

/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package com.thalesgroup.preferenceFramework.models.impl;

import java.util.Collection;


import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;

import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;

import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.impl.EObjectImpl;

import org.eclipse.emf.ecore.util.EObjectResolvingEList;
import org.eclipse.emf.ecore.util.EcoreUtil;

import com.thalesgroup.preferenceFramework.models.ModelsPackage;
import com.thalesgroup.preferenceFramework.models.Node;
import com.thalesgroup.preferenceFramework.models.NodeValue;
import com.thalesgroup.preferenceFramework.models.Outcome;
import com.thalesgroup.preferenceFramework.models.PreferenceNetwork;
import com.thalesgroup.preferenceFramework.models.exceptions.PreferenceException;

/**
 * <!-- begin-user-doc --> An implementation of the model object '<em><b>Outcome</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link com.thalesgroup.preferenceFramework.models.impl.OutcomeImpl#getName <em>Name</em>}</li>
 *   <li>{@link com.thalesgroup.preferenceFramework.models.impl.OutcomeImpl#getPreferenceNetwork <em>Preference Network</em>}</li>
 *   <li>{@link com.thalesgroup.preferenceFramework.models.impl.OutcomeImpl#getNodeValues <em>Node Values</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public abstract class OutcomeImpl extends EObjectImpl implements Outcome {
	/**
	 * The default value of the '{@link #getName() <em>Name</em>}' attribute.
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @see #getName()
	 * @generated
	 * @ordered
	 */
	protected static final String NAME_EDEFAULT = null;
	/**
	 * The cached value of the '{@link #getName() <em>Name</em>}' attribute.
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @see #getName()
	 * @generated
	 * @ordered
	 */
	protected String name = NAME_EDEFAULT;
	/**
	 * The cached value of the '{@link #getNodeValues() <em>Node Values</em>}' reference list.
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @see #getNodeValues()
	 * @generated
	 * @ordered
	 */
	protected EList<NodeValue> nodeValues;

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @generated
	 */
	protected OutcomeImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return ModelsPackage.Literals.OUTCOME;
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @generated
	 */
	public String getName() {
		return name;
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @generated
	 */
	public void setName(String newName) {
		String oldName = name;
		name = newName;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, ModelsPackage.OUTCOME__NAME, oldName, name));
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @generated
	 */
	public PreferenceNetwork getPreferenceNetwork() {
		if (eContainerFeatureID != ModelsPackage.OUTCOME__PREFERENCE_NETWORK) return null;
		return (PreferenceNetwork)eContainer();
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetPreferenceNetwork(
			PreferenceNetwork newPreferenceNetwork, NotificationChain msgs) {
		msgs = eBasicSetContainer((InternalEObject)newPreferenceNetwork, ModelsPackage.OUTCOME__PREFERENCE_NETWORK, msgs);
		return msgs;
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @generated
	 */
	public void setPreferenceNetwork(PreferenceNetwork newPreferenceNetwork) {
		if (newPreferenceNetwork != eInternalContainer() || (eContainerFeatureID != ModelsPackage.OUTCOME__PREFERENCE_NETWORK && newPreferenceNetwork != null)) {
			if (EcoreUtil.isAncestor(this, newPreferenceNetwork))
				throw new IllegalArgumentException("Recursive containment not allowed for " + toString());
			NotificationChain msgs = null;
			if (eInternalContainer() != null)
				msgs = eBasicRemoveFromContainer(msgs);
			if (newPreferenceNetwork != null)
				msgs = ((InternalEObject)newPreferenceNetwork).eInverseAdd(this, ModelsPackage.PREFERENCE_NETWORK__OUTCOMES, PreferenceNetwork.class, msgs);
			msgs = basicSetPreferenceNetwork(newPreferenceNetwork, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, ModelsPackage.OUTCOME__PREFERENCE_NETWORK, newPreferenceNetwork, newPreferenceNetwork));
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @generated
	 */
	public EList<NodeValue> getNodeValues() {
		if (nodeValues == null) {
			nodeValues = new EObjectResolvingEList<NodeValue>(NodeValue.class, this, ModelsPackage.OUTCOME__NODE_VALUES);
		}
		return nodeValues;
	}

	public NodeValue getNodeValue(Node node) throws PreferenceException {
		for (NodeValue nodeValue : nodeValues) {
			Node aNode = nodeValue.getNode();
			if(aNode.equals(node))
				return nodeValue;
		}
		
		throw new PreferenceException("Can't find value for node '"+ node.getName() +"' in this outcome !");
	}
	
	public EList<NodeValue> getParentValues(NodeValue childNodeValue) {
		EList<Node> parentNodes = childNodeValue.getNode().inputNodes();
		EList<NodeValue> nodeValues = this.getNodeValues();
		EList<NodeValue> parentValues = new BasicEList<NodeValue>();

		for (NodeValue nodeValue : nodeValues) {
			if(! nodeValue.equals(childNodeValue)) {
				Node aNode = nodeValue.getNode();
				if (parentNodes.contains(aNode)) {
					parentValues.add(nodeValue);
				}
			}
		}

		return parentValues;
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseAdd(InternalEObject otherEnd,
			int featureID, NotificationChain msgs) {
		switch (featureID) {
			case ModelsPackage.OUTCOME__PREFERENCE_NETWORK:
				if (eInternalContainer() != null)
					msgs = eBasicRemoveFromContainer(msgs);
				return basicSetPreferenceNetwork((PreferenceNetwork)otherEnd, msgs);
		}
		return super.eInverseAdd(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd,
			int featureID, NotificationChain msgs) {
		switch (featureID) {
			case ModelsPackage.OUTCOME__PREFERENCE_NETWORK:
				return basicSetPreferenceNetwork(null, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eBasicRemoveFromContainerFeature(
			NotificationChain msgs) {
		switch (eContainerFeatureID) {
			case ModelsPackage.OUTCOME__PREFERENCE_NETWORK:
				return eInternalContainer().eInverseRemove(this, ModelsPackage.PREFERENCE_NETWORK__OUTCOMES, PreferenceNetwork.class, msgs);
		}
		return super.eBasicRemoveFromContainerFeature(msgs);
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case ModelsPackage.OUTCOME__NAME:
				return getName();
			case ModelsPackage.OUTCOME__PREFERENCE_NETWORK:
				return getPreferenceNetwork();
			case ModelsPackage.OUTCOME__NODE_VALUES:
				return getNodeValues();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case ModelsPackage.OUTCOME__NAME:
				setName((String)newValue);
				return;
			case ModelsPackage.OUTCOME__PREFERENCE_NETWORK:
				setPreferenceNetwork((PreferenceNetwork)newValue);
				return;
			case ModelsPackage.OUTCOME__NODE_VALUES:
				getNodeValues().clear();
				getNodeValues().addAll((Collection<? extends NodeValue>)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case ModelsPackage.OUTCOME__NAME:
				setName(NAME_EDEFAULT);
				return;
			case ModelsPackage.OUTCOME__PREFERENCE_NETWORK:
				setPreferenceNetwork((PreferenceNetwork)null);
				return;
			case ModelsPackage.OUTCOME__NODE_VALUES:
				getNodeValues().clear();
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case ModelsPackage.OUTCOME__NAME:
				return NAME_EDEFAULT == null ? name != null : !NAME_EDEFAULT.equals(name);
			case ModelsPackage.OUTCOME__PREFERENCE_NETWORK:
				return getPreferenceNetwork() != null;
			case ModelsPackage.OUTCOME__NODE_VALUES:
				return nodeValues != null && !nodeValues.isEmpty();
		}
		return super.eIsSet(featureID);
	}

	/**
	 * Output the content of the outcome in a user understandable format.
	 * 
	 * @return the string generated from this outcome.
	 * @generated NOT
	 */
	@Override
	public String toString() {
		if (eIsProxy())
			return super.toString();

		StringBuffer result = new StringBuffer(this.getName());
		result.append("\t");
		EList<NodeValue> nodeValues = this.getNodeValues();
		for (NodeValue nodeValue : nodeValues) {
			result.append('(');
			result.append(nodeValue.getNode().getName());
			result.append(',');
			result.append(nodeValue.getName());
			result.append(")\t");
		}
		return result.toString();
	}

} // OutcomeImpl
